<?php
class BinderController extends BinderBaseController {
	public function qqReg(){
		$userInfo = HttpDaoManager::getUserDao()->get($_SESSION['user']['id']);
		if(!empty($userInfo)){
			if(!empty($userInfo['openid'])){
				if($userInfo['qq_bind_status']==QQ_BIND_FAIL){
					$userInfo['domain'] = '@qq.com';
					if(!empty($userInfo['qq'])){
						$userInfo['username'] = $userInfo['qq'];
					}
					if(!empty($userInfo['email'])){
						$username = substr($userInfo['email'],0,stripos($userInfo['email'],'@'));
						$userInfo['username'] = $username;
						$userInfo['domain'] = substr($userInfo['email'],stripos($userInfo['email'],'@'));
						if(!in_array($userInfo['domain'],array('@qq.com','@163.com','@126.com','@sina.com'))){
							$userInfo['username'] = '';
						}
					}
					$data = array();
					$data['userInfo'] = $userInfo;
					return $data;
				}elseif($userInfo['qq_bind_status']==QQ_BIND_UNCONFIRM){//邮箱确认中
					header('location:binder_qqReconfirm.htm');
				}else{
					header('location:index.htm');
				}
			}else{
				header('location:index.htm');
			}
		}else{
			header('location:register.htm');
		}
	}

	public function qqRegc(){
		$userInfo = HttpDaoManager::getUserDao()->get($_SESSION['user']['id']);
		if(!empty($userInfo)){
			if($userInfo['qq_bind_status']==QQ_BIND_UNCONFIRM){
				$userInfo = array();
				$userInfo['qq_bind_status'] = QQ_BIND_FAIL;
				$userInfo['qq_bind_id'] = 0;
				
				HttpDaoManager::getUserDao()->update($userInfo, $_SESSION['user']['id']);
				header('location:binder_qqReg.htm');
			}			
		}
	}
	
	
	public function subqqReg(){
		if(empty($_POST['qq'])||empty($_POST['username'])||empty($_POST['password'])||empty($_POST['cpassword'])){
			ClassManager::getToos()->msgbox('用户绑定错误,请重新绑定','binder_qqReg.html');
		}		
		$qq = trim($_POST['qq']);
		$email = trim($_POST['username']).strtolower(trim($_POST['domain']));
		if($_POST['password']!==$_POST['cpassword']){
			ClassManager::getToos()->msgbox('两次密码不一致,请重新输入','binder_qqReg.html');
		}
		
		$userEmailInfo  = HttpDaoManager::getUserDao()->getByEmail($email);
		if(empty($userEmailInfo)){//直接绑定
			$password = trim($_POST['password']);
			$userData = array();
			$userData['qq'] = $qq;
			$userData['email'] = $email;
			$userData['password'] = md5($password);
			$userData['qq_bind_status'] = QQ_BIND_SUCCESS;
			$userData['qq_bind_id'] = $_SESSION['user']['id'];
			HttpDaoManager::getUserDao()->update($userData, $_SESSION['user']['id']);
			unset($userData);
			ClassManager::getToos()->msgbox('绑定成功','user_home.html');
		}else{
			if($userEmailInfo['id']==$_SESSION['user']['id']){//自己绑定自己
				$password = trim($_POST['password']);
				$userData = array();
				$userData['qq'] = $qq;
				$userData['email'] = $email;
				$userData['password'] = md5($password);
				$userData['qq_bind_status'] = QQ_BIND_SUCCESS;
				$userData['qq_bind_id'] = $_SESSION['user']['id'];
				HttpDaoManager::getUserDao()->update($userData, $_SESSION['user']['id']);
				unset($userData);
				ClassManager::getToos()->msgbox('绑定成功','user_home.html');			
			}			
			
			if($userEmailInfo['qq_bind_status']==QQ_BIND_SUCCESS){
				ClassManager::getToos()->msgbox('该用户已被他人绑定，不能重复绑定,谢谢','binder_qqReg.html');
			}
			$password = trim($_POST['password']);
			$userData = array();
			$userData['qq'] = $qq;
			$userData['qq_bind_status'] = QQ_BIND_UNCONFIRM;
			$userData['password'] = md5($password);
			$userData['qq_bind_id'] = $userEmailInfo['id'];
			HttpDaoManager::getUserDao()->update($userData, $_SESSION['user']['id']);
			$_SESSION['session_qq_bind'] = $userEmailInfo;
			header('location:binder_qqVerify.htm');
		}
	}
	
	public function qqVerify(){
		if(empty($_SESSION['session_qq_bind'])){
			ClassManager::getToos()->msgbox('无效信息','binder_qqReg.html');
		}
		$userData = array();
		$userData['email'] = $_SESSION['session_qq_bind']['email'];
		return $userData;
	}
	
	public function qqReconfirm(){
		$userInfo = HttpDaoManager::getUserDao()->get($_SESSION['user']['id']);
		if(!empty($userInfo)){
			if($userInfo['qq_bind_status']==QQ_BIND_UNCONFIRM){
				$userBindInfo = HttpDaoManager::getUserDao()->get($userInfo['qq_bind_id']);
				$userData = array();
				$userData['email'] = $userBindInfo['email'];
				return $userData;
			}elseif($userInfo['qq_bind_status']==QQ_BIND_FAIL){
				header('location:index.htm');
			}else{
				header('location:user_home.htm');
			}
		}else{
			header('location:user_home.htm');
		}
	}
	
	public function subqqVerify(){
		$email = $_POST['email'];
		if(!empty($email)){
			$tail_url = "verify_id=".Common::encryptUrl('verify_'.$_SESSION['session_qq_bind']['id'],'128592');
			$verifyUrl = SITE_URL.'mailConfirm.htm?'.$tail_url;
			$mailContent ="";
			$mailContent .="<p>用户绑定验证链接,请点击";
			$mailContent .="<a href=\"".$verifyUrl."\">".$verifyUrl."</a>";
			$mailContent .="</p>";

			HttpServerManager::getEmailSendlistServer()->sendEmail($email, 'QQ绑定用户验证', $mailContent,1);
// 			$domain = substr($email,stripos($email,'@'));
// 			$url = 'mail.qq.com';
// 			if(stripos($domain, '163')>0){
// 				$url = 'mail.163.com';
// 			}
// 			if(stripos($domain, 'sina')>0){
// 				$url = 'mail.sina.com';
// 			}
// 			if(stripos($domain, '126')>0){
// 				$url = 'mail.126.com';
// 			}
			ClassManager::getToos()->msgbox('发送成功,请登录邮箱验证','index.htm');
		}else{
			ClassManager::getToos()->msgbox('无效信息','binder_qqReg.htm');
		}
	}
}
?>